package com.hengdu.cms.util;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class RabbitMQUtils {

	private static ConnectionFactory connectionFactory;

	static {

		connectionFactory = new ConnectionFactory();

		connectionFactory.setHost("192.168.57.130");
		connectionFactory.setPort(5672);
		connectionFactory.setVirtualHost("/ems");
		connectionFactory.setUsername("ems");
		connectionFactory.setPassword("ems@123");
	}

	// 提供链接对象
	public static Connection getConnection() {

		try {

			return connectionFactory.newConnection();
		} catch (IOException | TimeoutException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 释放资源
	public static void closeConnectionAndChanel(Channel channel, Connection connection) {

		try {

			if (null != channel) {
				channel.close();
			}
			if (null != connection) {
				connection.close();
			}
		} catch (IOException | TimeoutException e) {
			e.printStackTrace();
		}
	}
}
